package com.hb.game.pk.utils;

import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;

//import com.acker.simplezxing.activity.CaptureActivity;
import com.hb.game.pk.R;
import com.hb.game.pk.thread.DownLoadThread;
import com.hb.game.pk.utils.file.FilePathManager;

import java.io.File;
import java.util.concurrent.ExecutorService;

public class OpenMethod {
    public static int ScanPermissionCode = 11;
    public static int AlbumPermissionCode = 12;
//    public static int ScanCode = CaptureActivity.REQ_CODE;
//
//    public static void OpenScan(Activity act) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(act, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) {
//            act.requestPermissions(new String[]{Manifest.permission.CAMERA}, ScanCode);
//        } else {
//            act.startActivityForResult(new Intent(act, CaptureActivity.class), ScanCode);
//        }
//    }

//    public static String ScanResult(Activity act, int requestCode, int resultCode, Intent data) {
//        if (requestCode == CaptureActivity.REQ_CODE && resultCode == act.RESULT_OK) {
//            String scanInfo = data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT);
//            if (!StringUtils.isEmpty(scanInfo)) {
//                return scanInfo;
//            }
//        }
//        return "";
//    }

//    public static void scanPermissionResult(Activity act, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        if (requestCode == 11) {
//            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                act.startActivityForResult(new Intent(act, CaptureActivity.class), CaptureActivity.REQ_CODE);
//            }
//        }
//    }

    public static void startDownLoad(Activity act, String url, File saveFile, ExecutorService downLoadPool) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setTitle(act.getResources().getString(R.string.app_name));
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setMimeType("application/vnd.android.package-archive");
//                    request.setDescription("对于该请求文件的描述");
        saveFile = new File(FilePathManager.getUpdatePath(), act.getResources().getString(R.string.app_name_py) + ".apk");
        request.setDestinationUri(Uri.fromFile(saveFile));
        DownloadManager download = (DownloadManager) act.getSystemService(Context.DOWNLOAD_SERVICE);
        long downloadId = download.enqueue(request);
        DownloadManager.Query query = new DownloadManager.Query();
        downLoadPool.execute(new DownLoadThread(downloadId, download, query));
    }

    public static void install(Activity act, File saveFile) {
        Uri uri;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //如果是7.0以上的系统，要使用FileProvider的方式构建Uri
            uri = FileProvider.getUriForFile(act, "com.hm.retrofitrxjavademo.fileprovider", saveFile);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(saveFile), "application/vnd.android.package-archive");
        }
        act.startActivity(intent);
    }

    public static void openAlbum(Activity act, int addRequest) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (act.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                act.requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, AlbumPermissionCode);
            } else {
                ImageUtils.pickImageFromAlbum(act, addRequest);
            }
        } else {
            ImageUtils.pickImageFromAlbum(act, addRequest);
        }
    }

    public static void albumPermissionResult(Activity act, int addRequest, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == AlbumPermissionCode) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] ==
                    PackageManager.PERMISSION_GRANTED) {
                ImageUtils.pickImageFromAlbum(act, addRequest);
            }
        }
    }
}
